# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name： inspection
Description :
Author : 'li'
date： 6/26/2024
-------------------------------------------------
Change Activity:
6/26/2024:
-------------------------------------------------
"""
import ll
from ll.modules.network.cli_connection.ssh_client import SSHClient
from ll.modules.network.cli_connection.telnet_client import TelnetClient
from projects.inspection.network_device_inspection.modules.inspection.workflow.cmdb import load_device_login_info, load_inspection_commands
from projects.inspection.network_device_inspection.utils.command_util import load_inspection_commands_by_device_info


class Inspector:
    def __init__(
            self,
            inspection_device_area=('拓东', '西园')
    ):
        self.inspection_device_area = inspection_device_area
        self.device_info = load_device_login_info(inspection_device_area)
        self.inspection_commands = load_inspection_commands()

    def __call__(self):
        """
        Main function of inspector

        Args:

        Returns:

        """
        inspection_results = {}
        for info in self.device_info:
            connection = self._create_connection(info)
            if connection is None:
                ll.get_logger().warning(f'Failed to connect {info["ip"]}')
            self._inspection_one_device(connection, info)

    def _inspection_one_device(self, con, info):
        commands = load_inspection_commands_by_device_info(info, self.inspection_commands)
        pass

    @staticmethod
    def _create_connection(info):
        connection = None
        if info['connection_type'].upper() == 'SSH':
            c = SSHClient(
                ip=info['ip'],
                username=info['username'],
                password=info['password'],
                port=info['port'],
                device_type='generic'
            )
            if c.is_connected:
                connection = c
        elif info['connection_type'].upper() == 'TELNET':
            c = TelnetClient(
                ip=info['ip'],
                username=info['username'],
                password=info['password'],
                port=info['port'],
            )
            connection = c
        return connection


def _main():
    inspector = Inspector()
    inspector()
    pass


if __name__ == '__main__':
    _main()
